/*
 * @(#)XfsimLimitDialog.java        2003/01/01
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */


package xfuzzy.xfsim.view;

import xfuzzy.lang.*;
import xfuzzy.util.*;
import xfuzzy.xfsim.model.XfsimLimit;
import xfuzzy.xfsim.view.listener.XfsimLimitActionListener;
import javax.swing.*;
import java.awt.*;

/**
 * Di�logo para introducir los l�mites de un proceso de simulaci�n
 * 
 * @author Francisco Jos� Moreno Velo
 *
 */
public class XfsimLimitDialog extends JDialog {

	//----------------------------------------------------------------------------//
	//                            COSTANTES PRIVADAS                              //
	//----------------------------------------------------------------------------//

	/**
	 * C�digo asociado a la clase serializable
	 */
	private static final long serialVersionUID = 95505666603058L;
	
	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Ventana principal de la aplicaci�n
	 */
	private Xfsim xfsim;
	
	/**
	 * Campos para introducir los valores de los l�mites
	 */
//	private XTextForm text[];
	
	/**
	 * Campos para introducir los valores de los l�mites
	 */
	private XTextField field[];
	
	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor
	 */
	public XfsimLimitDialog(Xfsim xfsim) {
		super(xfsim,"Xfsim",true);
		this.xfsim = xfsim;
		build();
	}
	
	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//
		
	/**
	 * Selecciona las condiciones de parada de la simulaci�n
	 */
	public void set() {
		boolean good = true;
		double value[] = new double[field.length];
		boolean flag[] = new boolean[field.length];
		for(int i=0; i<field.length; i++) {
			if(field[i].getText().trim().length() > 0)
				try { value[i] = Double.parseDouble(field[i].getText()); flag[i] = true; }
			catch (NumberFormatException e) { good = false; field[i].setText(""); }
			else flag[i] = false;
		}
		if(good) {
			XfsimLimit limit = xfsim.getConfiguration().getLimit();
			int outputs = xfsim.getSpecification().getSystemModule().getOutputs().length;
			int inputs = xfsim.getSpecification().getSystemModule().getInputs().length;
			if(flag[0]) limit.setNLimit((int) value[0]);
			if(flag[1]) limit.setTLimit(value[1]);
			for(int i=0; i<outputs; i++) {
				if(flag[2*i+2]) limit.setOutputLowLimit(i, value[2*i+2]);
				if(flag[2*i+3]) limit.setOutputHighLimit(i, value[2*i+3]);
			}
			for(int i=0,base=2*outputs+2; i<inputs; i++) {
				if(flag[2*i+base]) limit.setInputLowLimit(i, value[2*i+base]);
				if(flag[2*i+base+1]) limit.setInputHighLimit(i, value[2*i+base+1]);
			}
			
			setVisible(false);
		}
		else Toolkit.getDefaultToolkit().beep();
	}
	
	/**
	 * Deselecciona las condiciones de parada de la simulaci�n
	 */
	public void unset() {
		xfsim.getConfiguration().getLimit().clearLimits();
		setVisible(false);
	}
	
	//----------------------------------------------------------------------------//
	//                             M�TODOS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Construye el contenido de la ventana
	 */
	private void build() {
		XfsimLimit limit = xfsim.getConfiguration().getLimit();
		Variable input[] = xfsim.getSpecification().getSystemModule().getInputs();
		Variable output[] = xfsim.getSpecification().getSystemModule().getOutputs();
		int length = 2+2*input.length+2*output.length;
		this.field = new XTextField[length];
		
		GridBagLayout layout = new GridBagLayout();
		JPanel panel = new JPanel();
		panel.setLayout(layout);
		addComponent(panel,layout,new XLabel("Simulation limits",450),0,0,3,1);
		addComponent(panel,layout,new XLabel("Variable",150),0,1,1,1);
		addComponent(panel,layout,new XLabel("Lower limit",150),1,1,1,1);
		addComponent(panel,layout,new XLabel("Upper limit",150),2,1,1,1);
		
		addComponent(panel,layout,new XLabel("_n",150),0,2,1,1);
		field[0] = new XTextField("",150);
		if(limit.isSetNLimit()) field[0].setText(""+limit.getNLimit());
		addComponent(panel,layout,field[0],2,2,1,1);
		
		addComponent(panel,layout,new XLabel("_t",150),0,3,1,1);
		field[1] = new XTextField("",150);
		if(limit.isSetTLimit()) field[1].setText(""+limit.getTLimit());
		addComponent(panel,layout,field[1],2,3,1,1);
		
		for(int i=0; i<output.length; i++) {
			addComponent(panel,layout,new XLabel(output[i].getName(),150),0,i+4,1,1);
			
			field[2*i+2] = new XTextField("",150);
			if(limit.isSetOutputLowLimit(i)) field[2*i+2].setText(""+limit.getOutputLowLimit(i));
			addComponent(panel,layout,field[2*i+2],1,i+4,1,1);
			
			field[2*i+3] = new XTextField("",150);
			if(limit.isSetOutputHighLimit(i)) field[2*i+3].setText(""+limit.getOutputHighLimit(i));
			addComponent(panel,layout,field[2*i+3],2,i+4,1,1);
		}
		int ibase = 2+2*output.length;
		int rbase = 4+output.length;
		for(int i=0; i<input.length; i++) {
			addComponent(panel,layout,new XLabel(input[i].getName(),150),0,i+rbase,1,1);

			field[2*i+ibase] = new XTextField("",150);
			if(limit.isSetInputLowLimit(i)) field[2*i+ibase].setText(""+limit.getInputLowLimit(i));
			addComponent(panel,layout,field[2*i+ibase],1,i+rbase,1,1);

			field[2*i+ibase+1] = new XTextField("",150);
			if(limit.isSetInputHighLimit(i)) field[2*i+ibase+1].setText(""+limit.getInputHighLimit(i));
			addComponent(panel,layout,field[2*i+ibase+1],2,i+rbase,1,1);
		}
		
		String lb[] = { "Set", "Unset", "Cancel" };
		XfsimLimitActionListener listener = new XfsimLimitActionListener(this);
		XCommandForm form = new XCommandForm(lb,lb,listener);
		form.setCommandWidth(100);
		
		Container content = getContentPane();
		content.setLayout(new BoxLayout(content,BoxLayout.Y_AXIS));
		content.add(panel);
		content.add(form);
		
		Point loc = xfsim.getLocationOnScreen();
		loc.x += 40; loc.y += 200;
		this.setLocation(loc);
		pack();
	}
	
	/**
	 * A�ade un componente al panel
	 * @param panel
	 * @param layout
	 * @param comp
	 * @param gridx
	 * @param gridy
	 * @param gridw
	 * @param gridh
	 */
	private void addComponent(JPanel panel, GridBagLayout layout, JComponent comp,int gridx,int gridy,int gridw,int gridh ) {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = gridx;
		gbc.gridy = gridy;
		gbc.gridwidth = gridw;
		gbc.gridheight = gridh;
		layout.setConstraints( comp,gbc );
		panel.add( comp );
	}

}

